

import Event from "../event"

const OverlayEventType = {
  CLICK: Cesium.ScreenSpaceEventType.LEFT_CLICK,
  DB_CLICK: Cesium.ScreenSpaceEventType.DOUBLE_CLICK,
  RIGHT_CLICK: Cesium.ScreenSpaceEventType.RIGHT_CLICK
}

export default class PrimitiveEvent extends Event {

  constructor() {
    super()
    this._viewer = undefined
    this._overlay = undefined
    this._registerEvent()
  }

  _registerEvent() {
    Object.keys(OverlayEventType).forEach(key => {
      let type = OverlayEventType[key]
      this._cache[type] = new Cesium.Event()
    })
  }

  /**
   * 注册鼠标事件 
   * @param {*} overlay 
   * @description 如果当前选择的primitve 与添加事件的相同一致 则返回
   */

  _setInputaction(overlay) {
    this._viewer = overlay._layer._viewer
    this._overlay = overlay
    this._handler = new Cesium.ScreenSpaceEventHandler(this._viewer.canvas)

    Object.keys(this._cache).forEach(key => {
      let type = this._cache[key]
      this._handler.setInputAction(movement => {
        let info = this.getMouseInfo(movement.position)

        if (!info.target) return
        if (info.target.primitive ) {
          if (info.target.primitive !== this._overlay.delegate) return
          type.raiseEvent(this._overlay , info)
        }

      }, key)
    })
  }

  /**
   * 销毁对象
   */
  destroy() {
    this._cache = {}
    this._viewer = undefined
    this._handler.destroy()
  }

  /**
   * 增加自定义事件
   */
  addToCache() { }
}